// pages/my/my.js
const app = getApp();
const {
  teamlist,
  changeTeam,
  exitTeam
} = require('../../config.js')
import Dialog from '../../dist/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    teams: [],
    teamTip: '',
    current: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.appRequest({
      url: teamlist,
      success: (res) => {
        console.log(res)
        let data = res.data.list;
        data.map( (item) => {
          item.isTouchMove = false;
        })
        this.setData({
          current: res.data.current,
          teams: data
        })
      }
    })

  },

  

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.appRequest({
      url: teamlist,
      success: (res) => {
        let data = res.data.list;
        if(data.length > 0){
          data.map((item) => {
            item.isTouchMove = false;
          })
          this.setData({
            current: res.data.current,
            teams: data,
            nolistTip: 0
          })
        }else{
          this.setData({
            nolistTip: -1
          })
        }
      }
    })
  },

  // 切换组织
  changeTeam: function (e) {
    let that = this;
    console.log(e.currentTarget.dataset.id, this.data.current)
    if (e.currentTarget.dataset.id == this.data.current){
      return
    }
    wx.showModal({
      title: '切换团队',
      content: '请您确认是否要切换团队',
      success(res) {
        if (res.confirm) {
          app.appRequest({
            url: changeTeam,
            method: 'POST',
            data: { merchantId: e.currentTarget.dataset.id },
            success: (res) => {
              // 刷新
              app.appRequest({
                url: teamlist,
                success: (res) => {
                  let data = res.data.list;
                  that.setData({
                    current: res.data.current,
                    teams: data
                  })
                }
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  onClose(event) {
    const { position, instance } = event.detail;
    console.log(event)
    if (position == 'right'){
      let that = this;
      // console.log(e.currentTarget.dataset.id, this.data.current)
      wx.showModal({
        title: '退出团队',
        content: '确认是否退出团队',
        success(res) {
          if (res.confirm) {
            app.appRequest({
              url: exitTeam,
              method: 'POST',
              data: { merchantId: event.currentTarget.dataset.id },
              success: (res) => {
                if (res.code == 0) {
                  // 刷新
                  app.appRequest({
                    url: teamlist,
                    success: (res) => {
                      let data = res.data.list;
                      that.setData({
                        current: res.data.current,
                        teams: data || ''
                      })
                    }
                  })
                } else {
                  wx.showToast({
                    title: res.msg,
                    icon: 'none'
                  })
                }
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
  }
})